<?php

namespace App\Domain\Material\Requests;

use App\Domain\Material\Dao\MaterialDao;
use Illuminate\Foundation\Http\FormRequest;

class WarehousingPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'SN' => ["required", function ($attribute, $value, $fail) {
                if ($value != '-1') {
                    $SNs = explode(',', $value);
                    foreach ($SNs as $v) {
                        $role = (new MaterialDao())::findBySN($v);
                    }
                    if ($role) {
                        $fail("SN已存在");
                    }
                }
            }],
            'material_dict_code' => 'required|string|nullable',
            'material_dict' => 'required|string|nullable',
            'name' => 'required|string|nullable',
            'model' => '',

        ];
    }

    public function messages()
    {
        return [
            'SN.required' => 'SN序列号必填',
            'material_dict_code.required' => '类型码必填',
            'material_dict.required' => '物资字典码必填',
            'name.required' => '物资名称必填',
        ];
    }
}
